import types from './types'
import { ApiFetchSystemsByAppId } from '@api/app'

const MockFetchSystemsByAppId = appId =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve({
        status: true,
        code: 0,
        message: null,
        data: [
          {
            id: 1,
            appId: 1,
            name: '1',
            path: '/main/permissionManagement',
            title: '数据采集管理平台',
            icon: 'iconic_quanxianguanli',
            children: null
          },
          {
            id: 2,
            appId: 1,
            name: '2',
            path: '/main/permissionManagement/roleManage',
            title: '数据标准管理平台',
            icon: 'iconic_jiaoseguanli',
            children: null
          }
        ],
        otherData: {}
      })
    }, 0)
  })

const MockFetchAsyncRoutes = appId =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve([
        {
          path: '/page1',
          icon: 'iconic_xiaoxi',
          title: 'page1',
          children: [
            {
              path: '/page1-1',
              icon: 'iconic_xiaoxi',
              title: 'page1-1',
              componentName: 'page1-1',
              roles: [1]
            },
            {
              path: '/page1-2',
              icon: 'iconic_xiaoxi',
              title: 'page1-2',
              componentName: 'page1-2',
              roles: [1]
            }
          ]
        },
        {
          path: '/page2',
          icon: 'iconic_xiaoxi',
          title: 'page2',
          componentName: 'page2'
        }
      ])
    }, 2000)
  })
export default {
  async fetchSystemsByAppId({ dispatch }, appId) {
    // let res = await ApiFetchSystemsByAppId(appId)
    let res = await MockFetchSystemsByAppId(appId)
    if (res.status && res.data) {
      dispatch('setSystems', res.data)
    }
  },
  async fetchAsyncRoutes({}, appId) {
    return await MockFetchAsyncRoutes(appId)
  },
  setAppId({ commit }, appId) {
    commit(types.SET_APP_ID, appId)
  },
  setSystems({ commit }, systems) {
    commit(types.SET_SYSTEMS, systems)
  },
  initApp({ commit }) {
    commit(types.INIT_APP)
  }
}
